<?php
/**
 * SweetPlurk
 *
 * @package    SweetPlurk
 * @copyright  Copyright (c) 2008-2012 Wabow Information Inc. (http://www.wabow.com)
 * @license    New BSD License
 * @version    $Id$
 */

/**
 * Plurks 集合
 *
 * @package    SweetPlurk
 * @copyright  Copyright (c) 2008-2012 Wabow Information Inc. (http://www.wabow.com)
 * @license    New BSD License
 */
class SweetPlurk_Plurks extends SweetPlurk_Go implements SeekableIterator, Countable, ArrayAccess
{
    /**
     * 指標
     *
     * @var int
     */
    protected $_pointer = 0;

    /**
     * 總數
     *
     * @var int
     */
    protected $_count = 0;

    /**
     * 資料列表
     *
     * @var array
     */
    protected $_list = array();

    /**
     * 使用者資訊
     *
     * @var SweetPlurk_Users
     */
    protected $_users = null;

    /**
     * 建構
     *
     * @param object $plurksData
     */
    public function __construct($plurksData)
    {
        $data = new stdClass();
        $data->plurks = $plurksData->plurks;
        parent::__construct($data);

        $this->_init();

        $data = new stdClass();

        if (isset($plurksData->plurks_users)) {
            $data->plurk_users = (array) $plurksData->plurks_users;
        } elseif (isset($plurksData->plurk_users)) {
            $data->plurk_users = (array) $plurksData->plurk_users;
        }
        $this->_users = new SweetPlurk_Users($data);
    }

    /**
     * 初始化
     *
     */
    protected function _init()
    {
        foreach ($this->plurks as $plurkData) {
            $this->_list[] = new SweetPlurk_Plurk($plurkData);
        }
        $this->_count = count($this->_list);
    }

    /**
     * 取得使用者列表
     *
     * @return SweetPlurk_Users
     */
    public function getUsers()
    {
        return $this->_users;
    }

    /**
     * 重置指標
     *
     * @return SweetPlurk_Plurks
     */
    public function rewind()
    {
        $this->_pointer = 0;
        return $this;
    }

    /**
     * 回傳目前指標指向的 Plurk
     *
     * @return SweetPlurk_Plurk
     */
    public function current()
    {
        if ($this->valid() === false) {
            return null;
        }

        if (empty($this->_list[$this->_pointer])) {
            $this->_list[$this->_pointer] = new SweetPlurk_Plurk(array(
                'lang' => null,
                'content_raw' => null,
                'responses_seen' => null,
                'qualifier' => null,
                'plurk_id' => null,
                'owner_id' => null,
                'response_count' => null,
                'plurk_type' => null,
                'content' => null,
                'qualifier_translated' => null,
                'user_id' => null,
                'posted' => null,
                'limited_to' => null,
                'no_comments' => null,
                'is_unread' => null,
            ));
        }

        return $this->_list[$this->_pointer];
    }

    /**
     * 回傳指標
     *
     * @return int
     */
    public function key()
    {
        return $this->_pointer;
    }

    /**
     * 指標指向下一筆
     *
     * @return void
     */
    public function next()
    {
        ++$this->_pointer;
    }

    /**
     * 檢查指標是否超過總數
     *
     * @return bool
     */
    public function valid()
    {
        return $this->_pointer < $this->_count;
    }

    /**
     * 移動指標
     *
     * @param int $position
     * @return SweetPlurk_Plurks
     */
    public function seek($position)
    {
        $position = (int) $position;
        if ($position < 0 || $position >= $this->_count) {
            throw new Exception("Illegal index $position");
        }
        $this->_pointer = $position;
        return $this;
    }

    /**
     * Plurk 總數
     *
     * @return int
     */
    public function count()
    {
        return count($this->_list);
    }

    /**
     * 特定 Plurk 是否存在
     *
     * @param int $offset
     * @return bool
     */
    public function offsetExists($offset)
    {
        return isset($this->_list[(int) $offset]);
    }

    /**
     * 取得目前指標指向的 Plurk
     *
     * @param int $offset
     * @return SweetPlurk_Plurk
     */
    public function offsetGet($offset)
    {
        $this->_pointer = (int) $offset;
        return $this->current();
    }

    /**
     * 不做任何事
     *
     * @param int $offset
     * @param mixed $value
     */
    public function offsetSet($offset, $value)
    {
    }

    /**
     * 不做任何事
     *
     * @param int $offset
     */
    public function offsetUnset($offset)
    {
    }
}